<?php
/**
 * 广告管理
 *
 * @category     Shuxun Cms
 * @date         2015年10月18日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         AdController.php
 */
namespace Scshux\Backend\Controllers;
use Scshux\Mvc\SModel;
use Scshux\Mvc\SQuery;
use Scshux\Utils\Tree;
use Scshux\Utils\Time;
use Scshux\Utils\Uploader;
class AdController extends  ControllerBase
{

	/**
	 * 菜单列表
	 */
	public function   indexAction()
	{
		$is_ajax  = $this->request->get('is_ajax','int',0);
		$dataList = $this->_getDataList();
		$this->view->setVar('dataList', $dataList);
		if($is_ajax){
			$this->view->setVar('full_page',0);
			$this->view->start();
			$this->view->render('ad','index');
			$this->view->finish();
			$content = $this->view->getContent();
			$data = array(
					'content' => $content,
					'filter'  => $dataList->filter,
					'totalPage' => $dataList->totalPage,
					'recordCount'=>$dataList->totalSize,
					'currentPage'=>$dataList->currentPage,
					'extended'   =>$dataList->extended
			);
			$this->sendResult(true,'',$data);
		}else{
			$this->view->setVar('full_page',1);
		}

	}

	/**
	 *  广告编辑
	 */
	public function   editAction()
	{
		$id = $this->request->get('id','int',0);
		if($this->request->isPost()){
			$adModel = new SModel('ad');
			
			//加入语言
			$_POST['site_id'] = $this->getCore()->getSiteID();
			$postData = $_POST;

			if($_FILES['content']){
				$uploaderObj = new Uploader();
				$postData['content'] =	$uploaderObj->upload($_FILES['content']);
				if(empty($postData['content'])){
					unset($postData['content']);
				}
			}
			if($id>0){
				$adModel->setData($postData);
				$adModel->update('id='.$id);
			}else{
				unset($postData['id']);
				$adModel->setData($postData);
				$adModel->add();
			}
			return $this->redirect('ad/index');
		}else{
			if($id>0){
				$adModel = new SModel('ad');
				$item = $adModel->getObj('id='.$id);
				$this->view->setVar('item', $item);
			}
			
			$adpositionModel = new SModel('ad_position');
			$adposition = $adpositionModel->query(false,'id,name','id','desc');
			$this->view->setVar('adposition',$adposition);
			
		}
	}

	/**
	 * 删除分类
	 */
	public  function  delAction()
	{
		$id = $this->request->get('id','int',0);
		if($id>0){
			$adModel = new SModel('ad');
			$adModel->del("id='{$id}'");
		}
		return $this->redirect('ad/index');
	}

	
	/**
	 * 广告位管理
	 */
	public function positionAction()
	{
	    $is_ajax  = $this->request->get('is_ajax','int',0);
	    $act  = $this->request->get('act');
	    if($act == 'remove'){
	        $this->_removeAdPosition();
	    }
	    $dataList = $this->_getAdPositionList();
	    $this->view->setVar('dataList', $dataList);
	    if($is_ajax){
	         
	        $this->view->setVar('full_page',0);
	        $this->view->start();
	        $this->view->render('ad','position');
	        $this->view->finish();
	        $content = $this->view->getContent();
	        $data = array(
	                'content' => $content,
	                'filter'  => $dataList->filter,
	                'totalPage' => $dataList->totalPage,
	                'recordCount'=>$dataList->totalSize,
	                'currentPage'=>$dataList->currentPage,
	        );
	        $this->sendResult(true,'',$data);
	    }else{
	        $this->view->setVar('full_page',1);
	    }
	}
	
	
	/**
	 * 编辑广告位
	 */
	public function adpositioneditAction()
	{
	    if ($this->request->isPost())
	    {
	        $id = isset($_POST['id']) && !empty($_POST['id']) ? intval($_POST['id']): '';
	        if ($id)
	        {
	            unset($_POST['id']);
	        }
	        $gstyleModel = new SModel('ad_position');
	        $data = array(
	                'name'=>(string)$_POST['name'],
	        );
	        $gstyleModel ->setData($data);
	        if ($id)
	        {
	            $gstyleModel->update('id='.$id);
	        }
	        else
	        {
	            $gstyleModel->add();
	        }
	        return $this->redirect('ad/position');
	    }
	    else
	    {
	        $id = isset($_GET['id']) && !empty($_GET['id']) ? intval($_GET['id']): 0;
	        if ($id)
	        {
	            unset($_GET['id']);
	        }
	        $gstyleModel = new SModel('ad_position');
	        $item = $gstyleModel->getObj('id='.$id);
	        $this->view->setVar('item', $item);
	    }
	}

	/**
	 * 删除广告位
	 */
	private function _removeAdPosition()
	{
	    $id = $this->request->get('id','int',0);
	    $ad_positionModel = new SModel('ad_position');
	    $ad_positionModel->del('id='.$id);
	}
	
	
	/**
	 * 列表数据
	 */
	protected   function  _getDataList()
	{
		$filter = array();
		$adModel = new SModel('ad');
		
		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();
		$where = 'site_id = '.$site_id;
		
		$items = $adModel->query($where,'*','sort desc,id','desc');

		$dataList = new \stdClass();
		$dataList->totalPage = 1;
		$dataList->currentPage = 1;
		$dataList->items = $items;
		$dataList->totalSize = count($items);
		$dataList->filter = $filter;
		return  $dataList;
	}

	
	private function _getAdPositionList()
	{
	    $filter = array();
	     
	    $page = $this->request->get('page','int',1);
	     
	    $where = '1=1';
	     
	     
	    $helpQuery = new SQuery('ad_position');
	    $helpQuery->fields = '*';
	    $helpQuery->page =  $page;
	    $helpQuery->where = $where;
	    $helpQuery->order = 'id desc';
	    $helpQuery->pagesize = 12;
	     
	    $items = $helpQuery->find();
	     
	    $dataList = new \stdClass();
	    $dataList->totalPage = $helpQuery->paging->getTotalPage();
	    $dataList->currentPage = $helpQuery->paging->getIndex();
	    $dataList->items = $items;
	    $dataList->totalSize = $helpQuery->paging->getTotalSize();
	    $dataList->filter = $filter;
	     
	    return  $dataList;
	}
	
}